package com.resou.reader.bookstore.category;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BookCategoryBean;
import com.resou.reader.api.entry.BookCategoryData;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.bookstore.category.view.ISubCategoryContract;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by ${Garrett} on 2018/9/29.
 * Contact me krouky@outlook.com
 */
public class SubCategoryPresenter extends ResouBasePresenter<ISubCategoryContract.ISubCategoryView> implements ISubCategoryContract.ISubCategoryPresenter {
    private SortSubBean sortSubBean;
    StoreService storeService;

    public SubCategoryPresenter(ISubCategoryContract.ISubCategoryView view, SortSubBean sortSubBean) {
        super(view);
        this.sortSubBean = sortSubBean;
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    @Override
    public void quearGateGoryData(String categoryId, String secId, String totalWord, String readingNum, String serialStatus, int page) {
        if (page == 0) {
            mView.getSmartRefreshLayout().setNoMoreData(false);
        }
        mView.showProgress();
        storeService.quearTypeBook(categoryId, secId, totalWord, readingNum, serialStatus, page, pageSize).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers()).subscribe(new Observer<Result<BookCategoryData>>() {
            @Override
            public void onSubscribe(Disposable d) {
                addCompositeDisposable(d);
            }

            @Override
            public void onNext(Result<BookCategoryData> bookCategoryDataResult) {
                if ((page + 1) * pageSize >= bookCategoryDataResult.getData().getTotalCount()) {
                    if (bookCategoryDataResult.getData().getTotalCount() == 0) {
                        mView.showEmpty();
                    }
                    mView.getSmartRefreshLayout().finishRefresh();
                    mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                    mView.showCateGoryData(bookCategoryDataResult.getData().getBookList());
                } else {
                    if (bookCategoryDataResult.getData() != null && bookCategoryDataResult.getData().getBookList() != null) {
                        mView.showCateGoryData(bookCategoryDataResult.getData().getBookList());
                    }
                    mView.getSmartRefreshLayout().finishLoadMore(true);
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.getSmartRefreshLayout().finishRefresh();
                mView.getSmartRefreshLayout().finishLoadMore();
                mView.showError(e);
            }

            @Override
            public void onComplete() {
                mView.showContent();
            }
        });
    }
}
